package com.golddaymicromall.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.golddaymicromall.util.ActivityController;
import com.golddaymicromall.util.IsNetUtils;
import com.lzy.okgo.OkGo;
import com.nispok.snackbar.Snackbar;
import com.nispok.snackbar.SnackbarManager;

/**
 * 处理所有activity共有逻辑类
 */
public abstract class BaseActivity extends AppCompatActivity {
    private IsNetUtils isnet;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityController.getInstance().addActivity(this);
        isnet = new IsNetUtils(BaseActivity.this);
        if (!isnet.IsNet()) {
            Toast.makeText(getApplicationContext(), "网络漫游中请查看是否有网", Toast.LENGTH_SHORT).show();
        } else {
            initList();
            initView();
        }

    }

    public void showError() {
        SnackbarManager.show(Snackbar.with(this).text("网络连接异常").duration(Snackbar.SnackbarDuration.LENGTH_SHORT));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityController.getInstance().removeActivity(this);


//        //根据 Tag 取消请求
        OkGo.getInstance().cancelTag(this);
//        //取消所有请求
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ActivityController.getInstance().removeActivity(this);
    }

    public void create(int LayoutID) {
        setContentView(LayoutID);
    }

    private void initView() {
    }

    private void initList() {
    }

    public void showToast(int msgID) {
        Toast toast = Toast.makeText(this, msgID, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public View f(int ViewId) {
        return findViewById(ViewId);
    }

    private ProgressDialog dialog;

    public void showLoading() {
        if (dialog != null && dialog.isShowing()) return;
        dialog = new ProgressDialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("加载中...");
        dialog.show();
    }

    public void dismissLoading() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

}
